﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.DomainRepository;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.ApllicationDto.Enums;
using Zocono.WMS.Infrastructure.BaseRepository;

namespace Zocono.WCS.Infrastructure.Repository
{
    public class EquipmentLayoutRepository : BaseRepository<EquipmentLayoutInfo>, IEquipmentLayoutRepository
    {
        public EquipmentLayoutRepository(SqlSugarClient context = null) : base(context)
        {

        }

        public async Task<PageDto<EquipmentLayoutInfoDto>> GetEquipmentLayoutAsync(Expression<Func<EquipmentLayoutInfo, DispatchEquipmentInfo, EquipmentLayoutTypeInfo, bool>> exp, PageModel page)
        {
            PageDto<EquipmentLayoutInfoDto> pageDto = new PageDto<EquipmentLayoutInfoDto>();
            RefAsync<int> totalNumber = 0;
            var result = await Context.Queryable<EquipmentLayoutInfo, DispatchEquipmentInfo, EquipmentLayoutTypeInfo>((a, b, c) => new JoinQueryInfos(
                JoinType.Left, a.EquipmentID == b.ID,
                JoinType.Left, a.EquipmentLayoutTypeID == c.ID))
                .Where(exp)
                .Select((a, b, c) => new EquipmentLayoutInfoDto
                {
                    ID = a.ID,
                    AreaCode = a.AreaCode,
                    ColumnCount = a.ColumnCount,
                    DefaultColor = a.DefaultColor,
                    Direction = a.Direction,
                    EquipmentID = a.EquipmentID,
                    EquipmentLayoutCode = a.EquipmentLayoutCode,
                    EquipmentLayoutTypeID = a.EquipmentLayoutTypeID,
                    PointX = a.PointX,
                    PointY = a.PointY,
                    SizeHeigth = a.SizeHeigth,
                    SizeWidth = a.SizeWidth,
                    EquipmentName = b.EquipmentName,
                    EquipmentLayoutTypeName = c.TypeName,

                }).ToPageListAsync(page.PageIndex, page.PageSize, totalNumber);

            pageDto.Result = result;
            pageDto.Total = totalNumber;
            return pageDto;
        }
    }
}
